
package messageDrivenBean;

import historyBean.*;
import javax.ejb.*;
import javax.naming.*;
import javax.jms.*;
import javax.rmi.*;

/**
 * Message Bean for working with history message
 * @author Konstantin
 */
public class MessageBean implements MessageDrivenBean, MessageListener {

    private transient MessageDrivenContext mdc = null;

    public MessageBean() {
        System.out.println("In SimpleMessageBean.SimpleMessageBean()");
    }

    public void setMessageDrivenContext(MessageDrivenContext mdc) {
        System.out.println("In "
            + "SimpleMessageBean.setMessageDrivenContext()");
	this.mdc = mdc;
    }

    public void ejbCreate() {
	System.out.println("In SimpleMessageBean.ejbCreate()");
    }

    public void onMessage(Message inMessage) {
        
        MapMessage historyMessage;
        Object homeHistoryObject;
        HistoryHome historyHome;
        History historyObject;
        
        try {
            
            if(inMessage instanceof MapMessage){
                
                historyMessage = (MapMessage) inMessage;
                homeHistoryObject = new InitialContext().lookup("HistoryBean");
                historyHome = (HistoryHome)PortableRemoteObject.narrow(
                homeHistoryObject,HistoryHome.class);
                historyObject = historyHome.create();
                
                historyObject.saveHistory(historyMessage);
            }
            else {
                System.out.println("Excepting "+ 
                        "MapMessage object, received "+
                        inMessage.getClass().getName());
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }    

    public void ejbRemove() {
        System.out.println("In SimpleMessageBean.remove()");
    }

}
